﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using YXQ.Common.Helper;
using YXQ.Dao.ViewModel;
using YXQ.Common.Extension;
using YXQ.Common.CacheManager;
using YXQ.Dao;
namespace YXQ.Service
{
    public class ControlService
    {

        public HttpContext Context { get; private set; }
        public string PostData { get; set; }

        public string StrValue { get; set; }
        public ControlService(HttpContext context)
        {
            Context = context;

            if (Context.Request["strValue"] != null)
                StrValue = Context.Request["strValue"];
            if (Context.Request["PostData"] != null)
                PostData = Context.Request["PostData"];
        }

        public ControlService()
        {
        }

        public string GetTop()
        {
            int page = 1;
            int rows = 5;
            int outrecord = 0;
            if (Context.Request["tpNum"] != null)
            {
                rows = Convert.ToInt32(Context.Request["tpNum"]);
            }

            string startTime = string.Empty;
            string endTime = string.Empty;
            string pname = string.Empty;



            List<ProjModel> pmdl = CenterService.projectService.GetPlist(pname,"", startTime, endTime, page - 1, rows, 0, out outrecord);
            return pmdl.ToJsonItem();

        }
        public string GetProJect()
        {

            int page = 0;
            int rows = 0;
            int outrecord = 0;
            string startTime = string.Empty;
            string endTime = string.Empty;
            string pname = string.Empty;
            string compName = string.Empty;

            if (Context.Request["page"] != null)
            {
                page = Convert.ToInt32(Context.Request["page"]);
            }
            if (Context.Request["rows"] != null)
            {
                rows = Convert.ToInt32(Context.Request["rows"]);
            }
            if (Context.Request["pname"] != null)
            {
                pname = Context.Request["pname"];
            }
            if (Context.Request["endTime"] != null)
            {
                endTime = Context.Request["endTime"];
            }
            if (Context.Request["startTime"] != null)
            {
                startTime = Context.Request["startTime"];
            }
            if (Context.Request["compName"] != null)
            {
                compName = Context.Request["compName"];
            }

            if (!string.IsNullOrEmpty(PostData))
            {

                List<ProjModel> pmdl = CenterService.projectService.GetPlist(pname, compName, startTime, endTime, page - 1, rows, Convert.ToInt32(PostData), out outrecord);
                return pmdl.ToJsonItem();
            }

            return "";
        }

        public string GetMessage() {

            int page = 0;
            int rows = 0;
            string qname = string.Empty;
            int outrecord = 0;
            if (Context.Request["page"] != null)
            {

                page = Convert.ToInt32(Context.Request["page"]);
            }
            if (Context.Request["rows"] != null)
            {

                rows = Convert.ToInt32(Context.Request["rows"]);
            }
            if (Context.Request["qname"] != null)
            {
                qname = Context.Request["qname"];
            }


            List<MessageModel> pmdl = CenterService.messageService.GetListModel(qname, page - 1, rows, out outrecord);
            if (pmdl != null)
            {
                return pmdl.ToJsonItem();
            }
            return "";
        }



        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <returns></returns>
        public string GetDepart()
        {
            int page = 0;
            int rows = 0;
            string depart = string.Empty;
            int outrecord = 0;
            if (Context.Request["page"] != null)
            {

                page = Convert.ToInt32(Context.Request["page"]);
            }
            if (Context.Request["rows"] != null)
            {

                rows = Convert.ToInt32(Context.Request["rows"]);
            }
            if (Context.Request["depart"] != null)
            {
                depart = Context.Request["depart"];
            }


            List<DepartModel> pmdl = CenterService.departService.GetListModel(depart, page - 1, rows, out outrecord);
            if (pmdl != null)
            {
                return pmdl.ToJsonItem();
            }
            return "";
        }

        public string GetMenu()
        {
            var uid = Context.Request["UId"];
            var uright = CenterService.uMService.GetOneByUId(Convert.ToInt32(uid));
            List<MudleModel> mdls = CenterService.mudleService.GetListByPid(0);
            List<TreeModel> trmdls = new List<TreeModel>();

            foreach (var m in mdls)
            {
                string chk = string.Empty;
                string sta = string.Empty;
                var sons = CenterService.mudleService.GetListByPid(m.Id);
                List<TreeModel> smdls = new List<TreeModel>();
                foreach (var son in sons)
                {
                    TreeModel trmdl = new TreeModel();
                    trmdl.id = son.FunCode;
                    trmdl.text = son.Title;
                    trmdl.state = "open";
                    if (uright != null)
                    {
                        if (uright.MudleId.Contains(son.FunCode.ToString()))
                        {
                            trmdl.check = "true";
                        }
                    }
                    smdls.Add(trmdl);
                    //  if(AdminUser)
                }

                if (sons.Count > 0)
                {
                    sta = "closed";
                }
                else
                {
                    sta = "open";
                }
                trmdls.Add(new TreeModel()
                {
                    id = m.FunCode,
                    text = m.Title,
                    state = sta,

                    children = smdls
                });
            }

            return trmdls.ToJsonItem();
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public string GetUser()
        {

            int page = 0;
            int rows = 0;
            int outrecord = 0;
            string strWhr = string.Empty;
            string realname = string.Empty;

            if (Context.Request["page"] != null)
            {

                page = Convert.ToInt32(Context.Request["page"]);
            }
            if (Context.Request["rows"] != null)
            {

                rows = Convert.ToInt32(Context.Request["rows"]);
            }
            if (Context.Request["Whr"] != null)
            {

                strWhr = Context.Request["Whr"];
            } if (Context.Request["RealName"] != null)
            {

                realname = Context.Request["RealName"];
            }



            List<UserModel> pmdl = CenterService.userService.GetList(strWhr, realname, page - 1, rows, out outrecord);
            if (pmdl != null)
            {
                return pmdl.ToJsonItem();
            }
            return "";
        }
        public string GerProJectById()
        {

            if (!string.IsNullOrEmpty(PostData))
            {
                ProjectModel pmdl = CenterService.projectService.GetOneById(Convert.ToInt32(PostData));
                return pmdl.ToJsonItem();
            }

            return "";

        }

        public string DelPro()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                if (CenterService.projectService.DelById(Convert.ToInt32(PostData)) > 0)
                {
                    return "Success";
                }
            }
            return "Failed";
        }

        public string DelUser()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                if (CenterService.userService.DelById(Convert.ToInt32(PostData)) > 0)
                {
                    return "Success";
                }
            }
            return "Failed";
        }

        public string DelDep()
        {

            if (!string.IsNullOrEmpty(PostData))
            {
                if (CenterService.departService.DelById(Convert.ToInt32(PostData)) > 0)
                {
                    return "Success";
                }
            }
            return "Failed";
        }
        public string DelMsg() {
            if (!string.IsNullOrEmpty(PostData))
            {
                if (CenterService.messageService.DelById(Convert.ToInt32(PostData)) > 0)
                {
                    return "Success";
                }
            }
            return "Failed";
        }

        public string SavePro()
        {
            if (!string.IsNullOrEmpty(PostData))
            {
                var usermodel = MembershipService.GetUser();
                ProjectModel pmd = this.PostData.FromJsonTo<ProjectModel>();
                pmd.CreateTime = DateTime.Now;
                pmd.IsDelete = 0;
                if (usermodel != null)
                {
                    if (pmd.Id == 0)
                    {


                        pmd.CreateUserName = usermodel.UserName;
                        pmd.CreateRealName = usermodel.RealName;
                        pmd.CreatUserId = usermodel.Id;
                    }
                    else
                    {
                        pmd.UpdateRealName = usermodel.RealName;
                        pmd.UpdateUserId = usermodel.Id;
                        pmd.UpdateUserName = usermodel.UserName;
                    }
                }
                if (CenterService.projectService.Save(pmd) > 0)
                {
                    return "Success";
                }

            }
            return "";
        }
        public string UserLogin()
        {
            if (StringUtils.GetWebConfigValue("IsTest") == "1")
            {
                MembershipService.ValidateUser("admin", "admin", 0);
                return "Success";
            }

            var chkCode = Context.Request["chkUser"];
            if (!string.IsNullOrEmpty(chkCode) && chkCode == "1")
            {

                StringUtils.SetCookie("User", "UserName", PostData);
            }
            else
            {
                StringUtils.DeleteCookie("User");
            }


            if (MembershipService.ValidateUser(PostData, StrValue, 0))
            {

                return "Success";
            }
            else
            {
                return "Failed";
            }
            //Session超时
            //return "SessionFailed";
        }

        #region 数据保存
        public string SaveProject()
        {
            
            
            string stRtn = string.Empty;
            UserModel umodel = MembershipService.GetUser();
            if (umodel != null)
            {
                if (!string.IsNullOrEmpty(PostData))
                {
                    switch (StrValue)
                    {
                            //报建部
                        case "ct1":
                            BJModel bjmodel = new BJModel();
                            bjmodel = this.PostData.FromJsonTo<BJModel>();
                            bjmodel.createUserid = umodel.Id;
                            bjmodel.createusername = umodel.UserName;
                            bjmodel.createrealname = umodel.RealName;
                            bjmodel.createtime = DateTime.Now; 
                            if (CenterService.bJService.Save(bjmodel) > 0)
                            {
                                stRtn = "Success";
                            } 
                            break;
                            //设计部
                        case "ct2":
                            SJModel sjmodel = new SJModel();
                            sjmodel = this.PostData.FromJsonTo<SJModel>(); 
                            sjmodel.createuserid = umodel.Id;
                            sjmodel.createusername = umodel.UserName;
                            sjmodel.createrealname = umodel.RealName;
                            sjmodel.createtime = DateTime.Now;

                            if (CenterService.sJService.Save(sjmodel) > 0)
                            {
                                stRtn = "Success";
                            } 
                            break;
                            //预算结算部
                        case "ct3":
                            YSJSModel ysjsmodel = new YSJSModel();
                            ysjsmodel = this.PostData.FromJsonTo<YSJSModel>(); 
                            ysjsmodel.createuserid = umodel.Id;
                            ysjsmodel.createusername = umodel.UserName;
                            ysjsmodel.createrealname = umodel.RealName;
                            ysjsmodel.createtime = DateTime.Now; 
                            if (CenterService.ySJSService.Save(ysjsmodel) > 0)
                            {
                                stRtn = "Success";
                            } 
                            break;
                            //招标办
                        case "ct4":
                            ZBBModel zbbmodel = new ZBBModel();
                            zbbmodel = this.PostData.FromJsonTo<ZBBModel>();
                            zbbmodel.createuserid = umodel.Id;
                            zbbmodel.createusername = umodel.UserName;
                            zbbmodel.createrealname = umodel.RealName;
                            zbbmodel.createtime = DateTime.Now;
                            //CWModel cwmodel = new CWModel();
                            //cwmodel.ProjectId = zbbmodel.ProjectId;
                            //cwmodel.gckyf =zbbmodel.zbje;
                            //cwmodel.jlfyf =zbbmodel.jlfwf;
                            //cwmodel.kcfyf =zbbmodel.kcfwf;
                            //cwmodel.sjfyf = zbbmodel.sjfwf;
                            //cwmodel.gsfyf = zbbmodel.gsfwf;
                            //cwmodel.zjzxfyf =zbbmodel.zxf;
                            //cwmodel.createuserid = umodel.Id;
                            //cwmodel.createusername = umodel.UserName;
                            //cwmodel.createrealname = umodel.RealName;
                            //cwmodel.createtime = DateTime.Now;
                            //CenterService.cWService.Save(cwmodel);

                            if (CenterService.zBBService.Save(zbbmodel) > 0)
                            {
                                stRtn = "Success";
                            } 
                            break;
                            //工程部
                        case "ct5":
                            GCBModel gcbmodel = new GCBModel();
                            gcbmodel = this.PostData.FromJsonTo<GCBModel>();
                            gcbmodel.createuserid = umodel.Id;
                            gcbmodel.createusername = umodel.UserName;
                            gcbmodel.createrealname = umodel.RealName;
                            gcbmodel.createtime = DateTime.Now;
                            if (CenterService.gCBService.Save(gcbmodel) > 0)
                            {
                                stRtn = "Success";
                            }
                            break;
                        //档案室
                        case "ct6":
                            DAModel damodel = new DAModel();
                            damodel = this.PostData.FromJsonTo<DAModel>();
                            damodel.createuserid = umodel.Id;
                            damodel.createusername = umodel.UserName;
                            damodel.createrealname = umodel.RealName;
                            damodel.createtime = DateTime.Now;
                            if (CenterService.dAService.Save(damodel) > 0)
                            {
                                stRtn = "Success";
                            }
                            break;
                        //财务室
                        case "ct7":
                            CWModel cwm = new CWModel();
                            

                            cwm = this.PostData.FromJsonTo<CWModel>();
                            CenterService.cWService.GetByProjectId(Convert.ToInt32( cwm.ProjectId));
                            
                            cwm.createuserid = umodel.Id;
                            cwm.createusername = umodel.UserName;
                            cwm.createrealname = umodel.RealName;
                            cwm.createtime = DateTime.Now;
                            if (CenterService.cWService.Save(cwm) > 0)
                            {
                                stRtn = "Success";
                            }
                            break;
                    }
                }
            }
            return stRtn;
        }

        #endregion

        #region  保存用户信息
        public string SaveUser() {
            if (!string.IsNullOrEmpty(PostData)) {
                UserModel umdl = this.PostData.FromJsonTo<UserModel>();
                if (!string.IsNullOrEmpty(umdl.Pwd))
                {
                    umdl.Pwd = StringUtils.EncryptPassWord(umdl.Pwd, StringUtils.PassFormat.MD5);
                }
                else {
                    umdl.Pwd = StringUtils.EncryptPassWord("123456", StringUtils.PassFormat.MD5);
                }
                if (CenterService.userService.Save(umdl) > 0) {
                    return "Success";
                }
            }
            return "";
        }
        #endregion


        #region  保存用户权限
        public string SaveUserRight() {
            if (!string.IsNullOrEmpty(PostData)) {
                UMModel um = this.PostData.FromJsonTo<UMModel>();
                if (CenterService.uMService.Save(um) > 0) {
                    return "Success";
                }
            }
            return "";
        }

        public string SaveDepart() {
            if (!string.IsNullOrEmpty(PostData))
            {
                DepartModel um = this.PostData.FromJsonTo<DepartModel>();
                if (CenterService.departService.Save(um) > 0)
                {
                    return "Success";
                }
            }
            return "";
        }
        public string SaveMessage() {
            UserModel user = MembershipService.GetUser();

            if (!string.IsNullOrEmpty(PostData))
            {
                MessageModel msg = this.PostData.FromJsonTo<MessageModel>();
                msg.isslave = 0;
                msg.slavetime = DateTime.Now;
                if (user != null) {
                    msg.createtime = DateTime.Now;
                    msg.createuserid = user.Id;
                    msg.createusername = user.UserName;
                }

                if (CenterService.messageService.Save(msg) > 0)
                {
                    return "Success";
                }
            }
            return "";
        }


        #endregion

        #region  获取金额
        public GridModel GetData() {
            GridModel gmdl = new GridModel();

            var start = Context.Request["starttime"];
            var end = Context.Request["endtime"];
            int mtype = 0;
            int ptype = 0;
            int page = 0;
            int rows = 0;
            int outrecord = 0;
            int proid = 0;
            string remark = string.Empty;
            if (Context.Request["rows"] != null)
            {
                rows = Convert.ToInt32(Context.Request["rows"]);
            }
            if (Context.Request["page"] != null)
            {
                page = Convert.ToInt32(Context.Request["page"]);
            }
            if (Context.Request["mtype"] != null)
            {
                mtype = Convert.ToInt32(Context.Request["mtype"]);
            }
            if (Context.Request["ptype"] != null)
            {
                ptype = Convert.ToInt32(Context.Request["ptype"]);
            }
            if (Context.Request["proid"] != null)
            {
                proid = Convert.ToInt32(Context.Request["proid"]);
            }
            if (Context.Request["remark"] != null)
            {
                remark =  Context.Request["remark"];
            }
            List<PayModel> pays = CenterService.payService.GetModel(remark, start, end, proid, mtype, ptype, page - 1, rows, out outrecord);
           var paymoney= pays.Sum(s => s.paymoney);
            gmdl.rows = pays.ToJsonItem();
            gmdl.total = outrecord;
            gmdl.footer = "\"paymoney\":\"合计：\",\"remark\":"+paymoney;
            return gmdl;

        }
        #endregion

        public string DelData() {
            int id = 0;
            if (Context.Request["id"] != null) {
                id = Convert.ToInt32(Context.Request["id"]);
            }
          return  (CenterService.payService.DelById(id)>0).ToString();
        }

        public GridModel SaveData() {
             
            PayModel pmdl = new PayModel();
            if (Context.Request["Id"] != null) {
                pmdl.id = Convert.ToInt32(Context.Request["Id"]);
            }
            if (Context.Request["mtype"] != null)
            {
                pmdl.mtype = Convert.ToInt32(Context.Request["mtype"]);
            }
            if (Context.Request["ptype"] != null)
            {
                pmdl.paytype = Convert.ToInt32(Context.Request["ptype"]);
            }
            if (Context.Request["projectid"] != null)
            {
                pmdl.ProjectId = Convert.ToInt32(Context.Request["projectid"]);
            }
            if (Context.Request["proid"] != null)
            {
                pmdl.ProjectId = Convert.ToInt32(Context.Request["proid"]);
            }

            if (Context.Request["paymoney"] != null)
            {
                pmdl.paymoney = Convert.ToInt32(Context.Request["paymoney"]);
            }
            if (Context.Request["remark"] != null)
            {
                pmdl.remark = Context.Request["remark"];
            }
            if (Context.Request["paydate"] != null)
            {
                pmdl.paydate = Convert.ToDateTime(Context.Request["paydate"]);
            }
            else {
                pmdl.paydate = DateTime.Now;
            }
            if (MembershipService.GetUser() != null) {
                if (!(pmdl.id > 0))
                {
                    pmdl.createtime = DateTime.Now;
                    pmdl.createuserid = MembershipService.GetUser().Id;
                    pmdl.createrealname = MembershipService.GetUser().RealName;
                    pmdl.createusername = MembershipService.GetUser().UserName;
                }
            }

            //保存新增或修改的数据
             CenterService.payService.Save(pmdl);

             int record = 0;
             List<Y_Pay> pays = CenterService.payService.GetList(0, 10, out record);
             GridModel gmdl = new GridModel();
             gmdl.rows = pays.ToJsonItem();
             gmdl.total = record;
             return gmdl;


        }


    }
}
